package com.bmit.clusterspring.controller;

import com.bmit.clusterspring.common.StatusCode;
import com.bmit.clusterspring.service.OutService;
import com.bmit.clusterspring.util.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/out")
public class OutController {
    @Autowired
    private OutService outService;

    @RequestMapping("/uploadreference")
    public StatusCode getReferenceCsv(HttpServletRequest request, @RequestParam MultipartFile file) {
        boolean isSuccess = false;
        if (!file.isEmpty()) {
            try {
                String ip = IpUtils.getIpAddr(request);
                isSuccess = outService.uploadReference(ip, file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return isSuccess ? StatusCode.Success : StatusCode.Fail;
    }

    @RequestMapping("/uploadresult")
    public StatusCode getResultCsv(HttpServletRequest request, @RequestParam MultipartFile file) {
        boolean isSuccess = false;
        if (!file.isEmpty()) {
            try {
                String ip = IpUtils.getIpAddr(request);
                isSuccess = outService.uploadResult(ip, file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return isSuccess ? StatusCode.Success : StatusCode.Fail;
    }

    @RequestMapping("/previewreference")
    public List<Map<String, String>> showReference(HttpServletRequest request) {
        String ip = IpUtils.getIpAddr(request);
        return outService.showReference(ip);
    }

    @RequestMapping("/previewresult")
    public List<Map<String, String>> showResult(HttpServletRequest request) {
        String ip = IpUtils.getIpAddr(request);
        return outService.showResult(ip);
    }

    @RequestMapping("/referencecolumns")
    public StatusCode referenceColumns(HttpServletRequest request, @RequestParam List<String> cols) {
        String ip = IpUtils.getIpAddr(request);
        outService.setReferenceColumns(ip, cols);
        return StatusCode.Success;
    }

    @RequestMapping("/resultcolumn")
    public StatusCode resultColumn(HttpServletRequest request, @RequestParam String col) {
        String ip = IpUtils.getIpAddr(request);
        outService.setResultColumn(ip, col);
        return StatusCode.Success;
    }

    @RequestMapping("/measure")
    public StatusCode cluster(HttpServletRequest request) {
        String ip = IpUtils.getIpAddr(request);
        outService.runMeasure(ip);
        return StatusCode.Success;
    }

    @RequestMapping("/result")
    public Map<String, String> getResult(HttpServletRequest request) {
        String ip = IpUtils.getIpAddr(request);
        return outService.getResult(ip);
    }
}
